package com.wangbo.box.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * 策略模式
 *
 * 该模式定义了一系列算法，并将每个算法封装起来，使它们可以相互替换，且算法的变化不会影响使用算法的客户。
 */
public class StrategyPattern {


    interface Strategy {
        void show();
    }

    static class StrategyA implements Strategy {

        @Override
        public void show() {
            System.out.println("StrategyA");
        }
    }

    static class StrategyB implements Strategy {

        @Override
        public void show() {
            System.out.println("StrategyB");
        }
    }

    static class StrategyContext{
        Map<String, Strategy> strategyMap;
        {
            strategyMap = new HashMap<>();
            strategyMap.putIfAbsent("A", new StrategyA());
            strategyMap.putIfAbsent("B", new StrategyB());

        }
        public Strategy getStrategy(String key) {
            return strategyMap.get(key);
        }
    }

    public static void main(String[] args) {
        StrategyContext context = new StrategyContext();
        Strategy strategyA = context.getStrategy("A");
        strategyA.show();
    }
}
